package com.UI;

import com.Model.StudentDBModel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddStudentWebmail extends Activity {
	private EditText txtWebAddress;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.add_student_webmail);
		
		// create a model instance
		final StudentDBModel newWebmail = new StudentDBModel(this);
		txtWebAddress = (EditText) findViewById(R.id.txtWebAddress);
		txtWebAddress.setText("http://");
		
		// register this activity as a button event listener
		Button btnSubmit = (Button) findViewById(R.id.btnWebmailSubmit);
		btnSubmit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
		 		EditText txtWAddress = (EditText)findViewById(R.id.txtWebAddress);
				String tempWAddress = txtWAddress.getText().toString().replaceAll(" ", "");
				
				if(tempWAddress.length() <= 7) {
			  	      
			 		   AlertDialog alertDialog = new AlertDialog.Builder(AddStudentWebmail.this).create();	        			
			 		   alertDialog.setTitle("Warning");	        		  
			 		   alertDialog.setMessage("Please Enter the Webmail Address");	        		  	        		  
			 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			 		  
			 		   public void onClick(DialogInterface dialog, int which) {
					       return;
					  } }); 
					  alertDialog.show();
					  
					}
					
				else {
				
					// Get the values from the user and send them to the DB
					ContentValues values = new ContentValues();

					values.put("WebmailAddress", txtWebAddress.getText().toString());

					int numWebmailsBefore = newWebmail.getNumWebmails();
					// use the model to store the data
					newWebmail.deleteAllWebmails();
					newWebmail.addNewStudentWebmail(values);

					// tell the user the assignment was added to the database
					int numWebmailsAfter = newWebmail.getNumWebmails();
				
					if(numWebmailsAfter == (numWebmailsBefore +1)){
						TextView lblResults = (TextView) findViewById(R.id.lblAddWebmailResult);
						lblResults.setText(getString(R.string.yesMail));
					 }
				
					//finish the activity to return to the calling event
					AddStudentWebmail.this.finish();
				}
			}
		});

		// register this activity as a button event listener
		Button btnGoHome = (Button) findViewById(R.id.btnHome);
		btnGoHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				//finish the activity to return to the calling event
				AddStudentWebmail.this.finish();
			}
		});
	}

}
